package controllers;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;
import javax.naming.InitialContext;
import javax.swing.JOptionPane;

import org.apache.bcel.generic.NEW;
import org.jfree.ui.RefineryUtilities;

import GUI.CustomerGui;
import GUI.LoginGUI;
import GUI.WorkerGUI;
import messages.ComplaintMessage;
import messages.LoginMessage;
import messages.LogoutMessage;
import messages.ReplayToClientMessage;
import messages.RequestUpdatePricesMessage;
import messages.SearchPicMessage;
import messages.SearchSampMessage;
import messages.SubscribingByCustomerMessage;
import messages.UpdatePricesMessage;
/*import Gui.ReturnsReport;
import Gui.LibrarianWindow;
import Gui.ActivityReport;
import Gui.AddBook;
import Gui.Chartmake;
import Gui.DirectorWindow;
import Gui.EditDetails;
import Gui.FreezeSubscriber;
import Gui.Histogram;
import Gui.LendHistogram;
import Gui.LendReport;
import Gui.LendingCard;
import Gui.MainScreen;
import Gui.ReturnsReport;
import Gui.Rigester;
import Gui.Search;
import Gui.SubscriberWindow;
import Gui.UpdateBookDetails;
import Gui.Viewlending;
import Gui.Viewsubscriberdetail;
import Gui.Workers;  */
import entity.UserLoginInfo;
//import Logic.TaskAssignment;
//import Gui.TheBookIsCopy;
import ocsf.client.AbstractClient;
import ocsf.server.ConnectionToClient;
/**
 * this class get answer from the server
 */

public class ClientController extends AbstractClient {
	public static Object togui;
	LoginMessage loginMsg;
	static String IP;
	static int po;
	UserLoginInfo logInfo=new UserLoginInfo();
	CustomerGui customerWindow;
	ConnectionToClient client;
	
	public ClientController(ConnectionToClient client){
		this.client=client;
		
	}
	
	public ClientController(String host, int port) throws IOException {
		super(host, port);
		openConnection();
	}
	
/**
 * This method handle to transfer the messages from the GUI
 */
	
	public void handleMessageFromGui(Object msg)
	  {
	    try
	    {
	    	sendToServer(msg);
	    	System.out.println("--client controller  "+msg.toString());
	    	
	    	String[] strMsg;
			  
			  msg= msg.toString().substring(1, msg.toString().length()-1); // remove the [] from the msg by substring 
			  strMsg = msg.toString().split("[ , ] "); 	// remove the , from msg and splitting it to tiny str array parts  
			  
			  System.out.println(strMsg[0]);
			  System.out.println(strMsg[1]);             
			  System.out.println(strMsg[2]);
			  
			  switch(strMsg[0]){
			  
			  case("Login"): {
				  LoginMessage loginMsg= new LoginMessage(strMsg,client);
				  loginMsg.activation();
				  break;
			  }
			  
			  case("Complaint"): {
				  ComplaintMessage complaintMsg=new ComplaintMessage(strMsg,client);
				  complaintMsg.activationComplaint();
				  break;
			  }
			  
			  
			  case("Logout"): {
				  LogoutMessage logoutMsg=new LogoutMessage(strMsg,client);
				  logoutMsg.activate(strMsg[1]);
				  break;
			  }
		
			  
			  case("subscribingByCustomer"):{
				  SubscribingByCustomerMessage subCustomer=new SubscribingByCustomerMessage(strMsg,client);
				  subCustomer.activationSub();
				  break;
			  }
			  			  				
			  case("SearchPic"):{
				  SearchPicMessage searchmsg=new SearchPicMessage(strMsg,client);
				  searchmsg.activationSearch();
				  break;
			  	}
			  
			  
			  case("RequestUpdatePrices"):{
				  RequestUpdatePricesMessage requestMsg=new RequestUpdatePricesMessage(strMsg,client);
				  requestMsg.activateRequest();
				  break;				  
			  }
			  
			  case("UpdatePrices"): {
				  UpdatePricesMessage updateMsg=new UpdatePricesMessage(strMsg,client);
				  updateMsg.activateUpdate();
				  break;				  
			  }
			  
			  case ("ReplayToClient"):{
				  ReplayToClientMessage replayMsg=new ReplayToClientMessage(strMsg,client);
				  replayMsg.activateReplay();
				  break;
			  }
			
			
			  case ("SearchSamp"):{
				  SearchSampMessage searchsmp=new SearchSampMessage(strMsg,client);
				  searchsmp.activationSearchsamp();
				  break;
			  }
			
	    	
	    	
	    	
	    	
	    	
			  }
	    	
	    	
	    	
	    }
	    catch(IOException e)
	    {
	 
	       System.out.println("Can't send message to server...  Terminating client...");
	      e.printStackTrace();
	      quit();
	    } catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	  }
	
	
	  
	  /**
	   * This method terminates the client.
	   */
	  public void quit()
	  {
	    try
	    {
	      closeConnection();
	    }
	    catch(IOException e) {}
	    System.exit(0);
	  }
	  
		@Override
		protected void handleMessageFromServer(Object msg) {
			String str=msg.toString();
			System.out.println("handleMessageFromServer ClientController:  "+str);
		
			if(msg.toString().equals("updateNewCustomer")){
				
				System.out.println("new customer wait for confirmation of customer service worker");
			}
			
			else if(msg.toString().equals("UpdateExistingCustomer")){
				
				System.out.println("Existing customer wait for confirmation of customer service worker");
			}
			
			else if(msg.toString().equals("RequestUpdatePrices")){
				String address="igal.f202@gmail.com";
				String messageTo="new prices updated in DB and wait for approval";
				
				
				System.out.println("new prices updated in DB and wait for approval");
			}
			
			else if(msg.toString().equals("UpdatePrices")){
				String address="igal.f202@gmail.com";
				String messageTo="new prices approved and updated in DB";
				SendMail mail=new SendMail(address,messageTo);
				
				System.out.println("new prices updated in DB");
			}
			
			else if(msg.toString().equals("complaintHandled")){
				ReplayToClientMessage rM=new ReplayToClientMessage();
				String address=rM.getEmail();
				String messageTo=rM.getMessageFromWorker();
				SendMail mail=new SendMail(address,messageTo);
			}

		}
	  
}


	
